package com.dhecp.project.api.enums;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @EnumName 亚马逊销售伙伴API端点枚举
 * @Description TODO
 * @Author alula
 * @Date 2022-12-01 15:43
 */
public enum RegionEnum {
    NORTH("NORTH","https://sellingpartnerapi-na.amazon.com", "us-east-1"),
    EUROPE("EUROPE","https://sellingpartnerapi-eu.amazon.com", "eu-west-1"),
    ASIA("ASIA","https://sellingpartnerapi-fe.amazon.com", "us-west-2");

    private String region;

    private String serviceUrl;

    private String awsRegion;

    RegionEnum(String region, String serviceUrl, String awsRegion) {
        this.region = region;
        this.serviceUrl = serviceUrl;
        this.awsRegion = awsRegion;
    }

    private static Map<String, RegionEnum> regionEnumMap = null;
    private static final Object lock = new Object();

    public static RegionEnum from(String region) {
        if (Objects.isNull(regionEnumMap)) {
            synchronized (lock) {
                if (Objects.isNull(regionEnumMap)) {
                    HashMap<String, RegionEnum> tempMap = new HashMap<String, RegionEnum>();
                    RegionEnum[] marketPlaces = RegionEnum.values();
                    if (Objects.nonNull(marketPlaces)) {
                        for (RegionEnum marketPlace : marketPlaces) {
                            tempMap.put(marketPlace.getRegion(), marketPlace);
                        }
                    }
                    regionEnumMap = tempMap;
                }
            }
        }
        return regionEnumMap.get(region);
    }



    public String getServiceUrl() {
        return serviceUrl;
    }

    public String getRegion() {
        return region;
    }

    public String getAwsRegion() {
        return awsRegion;
    }
}
